<template>
	<view class="cart-tool">
		<view class="tool_selectAll" v-if="showAllCheck">
			<checkbox-group @change="changeAllCheck">
				<label>
					<checkbox color="#D9557C" :checked="allChecked"/>
					<text>全选</text>
				</label>
			</checkbox-group>
		</view>
		<view class="total-price-box">
			<view class="total-price">合计：<text class="price-text">￥{{totalPrice}}</text></view>
			<view class="runway-text">包含运费</view>
		</view>
		<view class="settle" @tap="handleClick">{{buttonText}}({{totalNum}})</view>
	</view>
</template>

<script>
	export default {
		name:"ljy-settle",
		data() {
			return {
				
			};
		},
		props: {
			cartData: {
				type:Array,
				default:[]
			},
			buttonText: {
				type:String,
				default:'结算'
			},
			showAllCheck: {
				type:Boolean,
				default:false
			}
		},
		methods: {
			changeAllCheck() {
				this.$emit('changeAllCheck', !this.allChecked)
			},
			async handleClick() {
				if (this.buttonText == '结算') {
					const address = uni.getStorageSync('address');
					if (!address.userName) {
						uni.showToast({
							title: "您还没有获取收货地址",
							icon: 'none'
						});
						return;
					}
					if (this.totalNum === 0) {
						uni.showToast({
							title: "您还没有选中商品",
							icon: 'none'
						});
						return;
					}
					uni.navigateTo({
						url: '../../subpkg2/pay/pay'
					});
				} else if (this.buttonText == '支付') {
					try {
						//1.判断是否存在token地址
						const token = uni.getStorageSync('token');
						//2.在缓存记录跳转到登录页面前的页面是支付页面
						if (!token) {
							uni.setStorageSync('page', 'pay');
							uni.navigateTo({
								url: "../../subpkg2/login/login"
							});
							return;
						}
						// // 3.创建订单
						// const header ={
						// 	Authorization: token
						// };
						// //请求体参数
						// const address = uni.getStorageSync('address');
						// const addressAll=address.provinceName + address.cityName + address.countyName + address.datailInfo
						// const goods = [];
						// this.cartData.forEach(v => goods.push({
						// 	goods_id:v.goods_id,
						// 	goods_number: v.num,
						// 	goods_price:v.goods_price
						// }));
						// const orderParams = {
						// 	order_price:this.totalPrice,
						// 	consignee_addr:addressAll,
						// 	goods:goods
						// };
						// // 4.发出请求 创建订单 获取订单编写
						// // 5.发起 预支付接口
						// // 6.发起微信支付
						// // 7.查询后台 订单状态
						// // 8.提醒微信支付
						// //9.将支付成功商品从购物车中移除
						// // 10.跳转到订单页面
						uni.showToast({
							title: '支付成功',
							icon: 'none'
						});
			
						const buy = uni.getStorageSync('buy') || {};
						if (buy.goods_id) {
							uni.removeStorageSync('buy')
						} else {
							let cart = uni.getStorageSync('cart') || [];
							cart = cart.filter(v => !v.checked);
							uni.setStorageSync('cart', cart)
						}
			
						uni.reLaunch({
							url: '../../subpkg2/order/order'
						})
						// 9.将支付成功的商品从购物车中移除；
						// 10.跳转到订单页面
					} catch (e) {
						uni.showToast({
							title: '支付失败',
							icon: 'none'
						});
						console.log(err)
					}
			
				}
			
			}
		},
		computed: {
			allChecked() {
				return this.cartData.every(v => v.checked)
			},
			totalPrice() {
				let sum = 0;
				this.cartData.forEach(v => {
					if(v.checked) {
						sum += v.cartNum * v.goods_price
					}
				})
				return sum;
			},
			totalNum() {
				let num = 0;
				this.cartData.forEach(v => {
					if(v.checked) {
						num += v.cartNum
					}
				})
				return num;
			}
		}
	}
</script>

<style lang="scss">
	.cart-tool {
		position: fixed;
		bottom:0;
		width: 100%;
		height: 100rpx;
		background-color: #fff;
		display: flex;
		border-top: 1px solid #cacaca;
		
		.tool_selectAll {
			flex: 2;
			display: flex;
			justify-content: center;
			align-items: center;
		}
		.total-price-box {
			flex: 4;
			text-align: right;
			padding-right: 15rpx;
			
			.total-price {
				.price-text {
					color: #ff0000;
					font-size: 35rpx;
				}
			}
		}
		.settle {
			flex: 2;
			background-color: #ff0000;
			color: #fff;
			font-size: 32rpx;
			font-weight: 600;
			display: flex;
			justify-content: center;
			align-items: center;
		}
		
	}
</style>